package com.neudu.yyzx_service.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yyzx_service.common.JSONReturn;
import com.neudu.yyzx_service.pojo.Customer;
import com.neudu.yyzx_service.pojo.Nurserecord;
import com.neudu.yyzx_service.pojo.Outward;
import com.neudu.yyzx_service.service.CustomerService;
import com.neudu.yyzx_service.service.OutwardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class OutwardController {
    @Autowired
    private OutwardService outwardService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private JSONReturn jsonReturn;

    @RequestMapping("get_all_OutStatus")
    public String getAll(@RequestBody PageInfo pageInfo){
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            List<Outward> list = outwardService.selectAllOutStatus();
            System.out.println(list);
            PageInfo<Outward> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_outrecord_by")
    public String getByCondition(@RequestBody Outward Outward){
        try {
            List<Outward> list = outwardService.getOutRecord_by(Outward);
            return jsonReturn.returnSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }


    @RequestMapping("get_Isiner_customer")
    public String getInerCustomer(@RequestBody Map<String, Object> params){
        try {
            PageHelper.startPage((Integer) params.get("pageNum"),(Integer) params.get("pageSize"));
            List<Outward> list = outwardService.getIsinerCustomer((Integer) params.get("type"));
            PageInfo<Outward> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }


    @RequestMapping("add_out_record")
    public String addOne(@RequestBody Outward outward){
        try {
            outwardService.addOutRecord(outward);
            customerService.modifyOutStatus(outward.getCustomer().getCustomerName());
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("set_return_time")
    public String setReturnTime(@RequestBody Outward outward){
        try {
            outwardService.setReturnTime(outward);
            customerService.isback(outward);
            return jsonReturn.returnSuccess(outward);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

}
